package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserLikeService;
import com.tanhua.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserLikeService userLikeService;
    @Autowired
    private UserService userService;

    /**
     * 查询用户资料
     *    参数：token
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token,Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo 	userInfo,@RequestHeader("Authorization") String token) {
        userInfo.setId(UserHolder.getUserId());
        System.out.println("============================="+userInfo);
        userInfoService.update(userInfo);
        return ResponseEntity.ok("更新成功"+userInfo);
    }
    //更新用户头像

    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }

    //发送验证码

    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity SendVerificationCode() {
        userService.SendVerificationCode();
        return ResponseEntity.ok("发送成功");
    }

    //修改用户手机号-保存
    @PostMapping("/phone")
    public ResponseEntity Send(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }

    //修改用户手机号-校验验证码
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerficationCode(@RequestBody Map map) {
        String verificationCode = (String) map.get("verificationCode");

        //根据手机号获取验证码
        Boolean verification  =userService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(verification);

    }

    /**
     *查看互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("friends/{type}")
    public ResponseEntity friends(@RequestParam(defaultValue = "1")Integer page,
                                    @RequestParam(defaultValue = "10")Integer pagesize,
                                  String nickname,@PathVariable Integer type){
        PageResult pr = userInfoService.findFriends(page,pagesize,nickname,type);
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消喜欢
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLike(@PathVariable("uid") Long likeUserId){
        userLikeService.deleteLike(likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 对粉丝进行喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid")Long FansUserId){
        userLikeService.fansLike(FansUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo vo = userInfoService.fingCount();
        return ResponseEntity.ok(vo);
    }
}